import Vue from 'vue'
import router from './../router/index';
import {ToastPlugin} from 'vux'

Vue.use(ToastPlugin)

var tools = {

    baseUrl() {
        return 'http://yqsc.cqfuyuan.cn'
    },

    //跳转页面,不带查询参数
    goPage(path) {
        router.push({
            path: path
        })
    },

    //跳转页面,带查询参数,query为对象， {id:1}
    goPage_query(path, query) {
        scrollTo(0,0);
        router.push({
            path: path,
            query: query,
        })
    },

    // 保存参数到storage
    setStorageParam(type, text, value) {
        window[type].setItem(text, value)
    },

    // 获取保存到storage的参数
    getStorageParam(type, text) {
        return window[type].getItem(text)
    },

    // 获取url参数
    getParams(params) {
        let url = decodeURI(location.href);
        let paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");

        for (let i = 0; i < paraString.length; i++) {
            let _key = paraString[i].split('=')[0];
            let _val = paraString[i].split('=')[1];
            if (_key === params) return _val;
        }
        return '';
    },

    // 将日期转化为时间搓
    changeTimeToNumber(time) {
        let date = new Date(time);
        return date.getTime();
    },

    // 上传文件,上传成功后直接取sessionStorage里面的uploadImg
    uploadFiles(url, form) {
        $.ajax({
            url: tools.baseUrl() + url,
            type: "post",
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {
                window.sessionStorage.setItem('uploadImg', data.data)
                return data
            },
            error: function (e) {
                Vue.$vux.toast.show({type: 'text', text: '上传图片失败,请稍再试', position: 'middle',})
                return ''
            }
        });
    },

    // 上传图片
    previewImg(e, showId, inputId, imgWidth) {
        let _showId = '#' + showId;
        let files = e.target.files;
        let tempWidth;
        let tempHeight;
        let type = "";
        if (files[0].type) {
            let _index = files[0].type.indexOf('/');
            type = files[0].type.slice(_index + 1, files[0].type.length)
        }
        let img = new Image;
        if (window.URL) {
            img.src = window.URL.createObjectURL(files[0]);
            let size = files[0].size;
            img.onload = function (e) {
                window.URL.revokeObjectURL(img.src);
                tempWidth = imgWidth;
                tempHeight = img.height * tempWidth / img.width;
                img.style.width = '100%';
                img.style.height = tempHeight + 'px';
                img.style.maxHeight = '240px';
                img.alt = img.width + 'x' + img.height
                $(_showId).append(img);
            }
        } else {
            e.target.select();
            e.target.blur();
            let nfile = document.selection.createRange().text;
            document.selection.empty();
            img.src = nfile;
            img.onload = function () {
            }
            $(_showId).append(img);
        }
    },

    // 删除上传的图片
    deleteImg(clear_file, remove_img) {
        $(clear_file).val('')
        $(remove_img).children('img').remove();
    },

    // 必填字段判断

    //浮点数乘法，计算商品价格
    accMul(arg1, arg2) {
        let m = 0;
        let s1 = arg1.toString();
        let s2 = arg2.toString();
        try {
            m += s1.split(".")[1].length
        } catch (e) {
        }
        try {
            m += s2.split(".")[1].length
        } catch (e) {
        }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    },

    //浮点数加减法
    floatSub(arg1, arg2, symbol) {
        let r1, r2, m, n;
        try {
            r1 = arg1.toString().split(".")[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        } catch (e) {
            r2 = 0;
        }
        m = Math.pow(10, Math.max(r1, r2));
        //动态控制精度长度
        //                n = (r1 = r2) ? r1 : r2;
        n = 2;
        return symbol === "-"
            ? ((arg1 * m - arg2 * m) / m).toFixed(n)
            : ((arg1 * m + arg2 * m) / m).toFixed(n);
    },

}

export default tools

